Programming Cloud

Joel F. Klein

Wolfram Research

Topics

• Types of deployments
• Development environment options
• Programming environment
• Debugging tools: messages, profiling, and logging
• CloudLoggingData and UsageDashboard

Deployments

Types of Deployments

• APIs
• Web computations
• Forms
• CloudCDFs
• Scheduled Tasks

CloudDeploy

CloudDeploy[APIFunction[...]]
CloudDeploy[Delayed[...]]
CloudDeploy[FormFunction[...]]
CloudDeploy[Manipulate[...]]
CloudDeploy[ScheduledTask[...]]
CloudDeploy[ExternalBundle[...]]

Note on saving definitions

CloudDeploy automatically saves definitions:
   you can say APIFunction[{}, myAlgorithm[...]] and myAlgorithm and its dependencies will be saved.

Development Environment Options

• Desktop, offline and online
• Online in browser
• Note: Private cloud could combine desktop+online

Desktop Development

• Advantage: generally faster, smoother
• Advantage: can work with Wolfram Workbench: source control tools, unit testing
• Disadvantage: has to be installed

Online in Browser

• Advantage: no network overhead in modifying cloud objects
• Advantage: programming environment is closer to the deployment environment

Private Cloud

• Desktop application + Wolfram Workbench + cloud server colocated

Wolfram cloud developers essentially do this

Programming Environment

File System

• $HomeDirectory, Directory[] are the cloud file system, network file system
• $TemporaryDirectory is local to the server, may change between notebook sessions, API calls, etc.
• Files created under $HomeDirectory can be accessed as cloud objects and vice versa.

Evaluation Environment

$CloudEvaluation

$EvaluationEnvironment

$EvaluationCloudObject (* in the cloud, is set to current notebook, API, scheduled task, etc. *)

Kernel is initialized based on evaluation enironment:
  • When developing in interactive noteook session, evaluated as the logged-in user
  • Deployments are evaluated as owning user, with info about the requester
  • $GeoLocation and locale-specifics such as $DateStringFormat are from the requesting user’s perspective

See the Cloud Execution Metadata guide page (guide/CloudExecutionMetadata) for lots more information.

User Metadata Demo

environmentTable[args___] := TableForm@Map[{#, Symbol[#]}&,{"$WolframID","$RequesterWolframID", "$RequesterAddress", "$GeoLocation", "$GeoLocationCountry", "$GeoLocationCity", "$TimeZone","$DateStringFormat"}]

environmentTable[]

whoami=CloudDeploy[Delayed[environmentTable[],"PNG"],"whoami"]

SaveDefinitions Behavior of CloudDeploy

Import[whoami,"Text"]

Debugging Tools

• ResponseForm 
• Logging
• TracePrint, TraceScan, Stack

ResponseForm

ResponseForm gives you the result PLUS evaluation metadata and artifacts, including messages.

factorApi = CloudDeploy[APIFunction[{"x"},FactorInteger[#n]&], "factor"]

Visit the URL directly, and you get the HTML ResponseForm page allowing you to supply the expected parameters.

Even when you supply parameters, you get an odd response:

ResponseForm via Request Parameter

Ask for a ResponseForm with a parameter:
https://www.wolframcloud.com/objects/user-11bcb0ff-b342-47f5-9261-c723dd93c852/factor?x=23&_responseform=JSON

Now we see the problem: the parameter was declared as “x” but referenced as “n”.

We also have other useful information here, Timing and AbsoluteTiming.

The information comes from EvaluationData:

EvaluationData[1/0]

Logging

Server-side programming uses logging:
• debugging
• flag exceptional conditions
• usage statistics

Logging Demo

Write to an area under $HomeDirectory, not $TemporaryDirectory.

sentimentApi = CloudDeploy[
APIFunction[{"q"}, 
With[{result=Classify["Sentiment", #q], log = OpenAppend["sentiment.log"]},
WriteString[log,DateString[]<>" "<>$RequesterWolframID<>" "<>$RequesterAddress<>" "<>ToString[First@$GeoLocation]<>" "];
WriteString[log, #q<>"\n"];
Close[log];
result
]&],
"sentiment",Permissions->"Public"
]

URLFetch[sentimentApi,"Parameters"->{"q"->"Dropped my keys down a storm drain"}]

URLFetch[sentimentApi,"Parameters"->{"q"->"So bummed; my car keys are down a storm drain"}]

URLFetch[sentimentApi,"Parameters"->{"q"->"I fished my keys out! Kat is a regular Macguyver!"}]

CloudEvaluate[Import["sentiment.log", "Text"]]

CloudEvaluate[DeleteFile["sentiment.log"]]

Usage Dashboard

Automatically monitors calls and credit usage.